// pages/Scan/Scan.js
const httpRequest = require('../../utils/http_utils.js')
const conn = require('../../utils/common.js')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '',
    code: '', //扫描的编码
    dailyList: [], //日常巡检集合
    error: "", //失败提示
    info: '', //提示信息
    customHeight: 0, //自定义高度
  },

  //扫一扫获取任务列表
  ResultList: function() {
    var that = this;
    var param = {
      RowGuid: that.data.code
    }
    console.log(JSON.stringify(param) + "---------------上传参数")
    httpRequest.GetValidateRequest(httpRequest.apiInfo.GetProjectXjFormMobileApi, param,
      function(data) {
        that.data.dailyList = [];
        var TableList = that.data.dailyList;
        if (that.data.code.length > 14) {
          console.log(JSON.stringify(JSON.parse(data.Table3)) + "---------------------------接口返回参数3")
          var lengthTab3 = JSON.parse(data.Table3);
          console.log(lengthTab3.length+"-----------------接口长度")
          if (lengthTab3.length > 0) {
            for (var i = 0; i < lengthTab3.length; i++) {

              var res = "/";
              try {
                res = "每" + conn.getDicText("巡检周期单位", lengthTab3[i].InspectUnit) + lengthTab3[i].InspectTime + "次" + "(第" + lengthTab3[i].TaskIndex + "次巡检)";
              } catch (e) {
                res = "/";
              }
              var params = {
                TaskName: lengthTab3[i].TaskName,
                Location: lengthTab3[i].Location,
                IsAlarm: lengthTab3[i].IsAlarm,
                TaskType: 2,
                RowGuid: lengthTab3[i].RowGuid,
                RoomName: lengthTab3[i].EquipName,
                cycle: res
              }
              TableList.push(params)


              // TableList.push(lengthTab3[i])
            }
            that.setData({
              dailyList: TableList,
            })
          } else {
            that.setData({
              info: '当前暂无巡检任务！'
            })
            setTimeout(() => {
              wx.navigateBack({
                delta: 1
              })
            }, 1000)
          }
        } else if (that.data.code.length == 14) {
          console.log(JSON.stringify(JSON.parse(data.Table5)) + "---------------------------接口返回参数5")
          var lengthTab5 = JSON.parse(data.Table5);
          console.log(lengthTab5.length + "-----------------接口长度")
          that.setData({
            info: lengthTab5.length + '条新的巡检任务！'
          })
          if (lengthTab5.length > 0) {
            for (var i = 0; i < lengthTab5.length; i++) {


              var res = "/";
              try {
                res = "每" + conn.getDicText("巡检周期单位", lengthTab5[i].InspectUnit) + lengthTab5[i].InspectTime + "次" + "(第" + lengthTab5[i].TaskIndex + "次巡检)";
              } catch (e) {
                res = "/";
              }
              var params = {
                TaskName: lengthTab5[i].TaskName,
                Location: lengthTab5[i].Location,
                IsAlarm: lengthTab5[i].IsAlarm,
                TaskType: 1,
                RowGuid: lengthTab5[i].RowGuid,
                RoomName: lengthTab5[i].Room,
                cycle: res
              }
              TableList.push(params)

              // TableList.push(lengthTab5[i])
            }
            that.setData({
              dailyList: TableList,
            })
            console.log(JSON.stringify(that.data.dailyList) + "-----------------------扫一扫返回集合")
          } else {
            that.setData({
              info: '当前暂无巡检任务！'
            })
            setTimeout(() => {
              wx.navigateBack({
                delta: 1
              })
            }, 1000)
          }
        }
      },
      function(err) {})
  },

  //列表点击事件
  addPage: function (data) {
    console.log(this.data.clickEvents);
    if (this.data.clickEvents == false) {
      return
    } else {

      console.log(JSON.stringify(data.currentTarget.dataset))
      if (data.currentTarget.dataset.tasktype == "1" || data.currentTarget.dataset.tasktype == 1) {
        wx.navigateTo({
          url: '../xunJianAdd/xunJianAdd?GuidType=' + 'Room' + '&TaskId=' + data.currentTarget.dataset.rowguid + '&RoomName=' + data.currentTarget.dataset.roomname,
        })
      } else if (data.currentTarget.dataset.tasktype == "2" || data.currentTarget.dataset.tasktype == 2) {
        wx.navigateTo({
          url: '../xunJianAdd/xunJianAdd?GuidType=' + 'Equip' + '&TaskId=' + data.currentTarget.dataset.rowguid + '&RoomName=' + data.currentTarget.dataset.roomname,
        })
      }

    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      code: options.code,
      customHeight: (app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2
    })
    this.ResultList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})